package com.orange.user.controller;

import com.orange.user.annotation.AuthCheck;
import com.orange.user.common.ErrorCode;
import com.orange.user.common.Response;
import com.orange.user.exception.BusinessException;
import com.orange.user.model.*;
import com.orange.user.service.UserService;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Data
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Response<Long> register(@RequestBody UserRegisterRequest userRegisterRequest) throws InterruptedException {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long result = userService.register(userRegisterRequest);
        return Response.newSuccess(result);
    }

    @PostMapping("/login")
    public Response<?> login(@RequestBody UserLoginRequest userLoginRequest) {
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        String token = userService.login(userAccount, userPassword);
        return Response.newSuccess(token);
    }

    @PostMapping("/validate")
    public TokenValidationResponse validate(@RequestBody TokenValidationRequest request) {
        return userService.validateToken(request);
    }

    @PostMapping("/update")
    public Response<String> update(@RequestBody UserUpdateRequest userUpdateRequest) {
        return Response.newSuccess(userService.update(userUpdateRequest));
    }

    @PostMapping("/delete")
    public Response<String> delete() {
        return Response.newSuccess(userService.delete());
    }

    @GetMapping("/info/{id}")
    public Response<UserVO> info(@PathVariable Long id) {
        return Response.newSuccess(userService.getUserVO(userService.getById(id)));
    }

    @GetMapping("/list")
    @AuthCheck(mustRole = "admin")
    public Response<List<UserVO>> list(@RequestParam short page, @RequestParam short size) {
        return Response.newSuccess(userService.pageUsers(page,size));
    }


}
